Skip to content

Give new T[a][b] the length of its first dimension - #559

Open
CatarinaGamboa wants to merge 2 commits into
mainfrom
fix/557-multidim-array-length
Open

CatarinaGamboa wants to merge 2 commits into
mainfrom
fix/557-multidim-array-length

Conversation

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator

Fixes #557

Problem

visitCtNewArray looped over every dimension expression and kept the last one, so new int[2][3] got length(_) == 3. Programs that relied on the wrong row count were accepted.

Fix

Only the first dimension sets the length: new T[d1][d2]... gets length(_) == d1. Inner arrays are not modelled, so nothing is assumed about their lengths. The other dimension expressions are still visited as before; they are just no longer tied to length.

Array initializers were already right (new int[][] {{1,2},{3}} has length 2, the number of rows), and 1-D creation is unchanged.

Cases checked (by hand, not all added as tests)

Rejected now: new int[2][3] / new int[2][] / new int[5][6][] / new int[5][6][7] passed where the row count is a later dimension; new int[n][m] against m; {{1,2,3}} against 3; reassignment g = new int[2][3]; return and field refinements length(_) == 3 on new int[2][3]; new String[2][4] assigned to Object[][]; length(_) > 1 with new int[n][5] and only n > 0; inner-row lengths (g[0] against 3) stay unknown.
Accepted: the same shapes with the first dimension, initializers, 1-D new int[9], {1,2}, {}.

Tests

CorrectStringArrayLength and ErrorStringArrayLength each get one 2-D line; both fail on main and pass with the fix. Full mvn test passes.

🤖 Generated with Claude Code

A multi-dimensional array creation took the length of its last dimension,
so length(g) of new int[2][3] was 3 and wrong programs were accepted. Only
the first dimension now sets the length; inner arrays stay unmodelled.

Fixes #557

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 1

Codex ran 39 small programs with ./liquidjava (multi-dimensional creations with literal and variable dimensions, partial dimensions such as new int[2][], nested initializers, reassignments, annotated locals, fields and returns, casts, nested creations, 1-D controls).

Result: no soundness bug or crash introduced by this PR. Every wrong outer-length claim was rejected, e.g.

  • field @Refinement("length(_) == 3") int[][] a = new int[2][3] → found length(this.a) == 2 but expected length(this.a) == 3
  • return length(_) == 3 of new int[2][3] → found length(#ret) == 2 ...
  • nested initializer {{1,2,3},{4,5,6}} against 3 → found n == 3 && length(a) == 2 ...

The matching programs and the 1-D controls pass.

One finding is pre-existing on main (same output before this PR), so it is out of scope here:

  • a conditional whose branches are arrays, need(b ? new int[2][3] : new int[2][4], 2), fails with Error: Sorts |int[][]| and Bool are incompatible.

Nothing to fix in this PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Clean-code review

Audit of the diff with the clean-code-principles checklist (SOLID, DRY, KISS, YAGNI, naming, functions, comments).

RefinementTypeChecker.java:265 - [name-meaningful] `exp` hides that only the first dimension is used; renamed to `outerDimension`. (applied)
CorrectStringArrayLength.java:29 - [name-meaningful] the 2-D call sat in `initializers()` but is not an initializer; moved to its own `dimensions()` method. (applied)
RefinementTypeChecker.java:259-275 - [core-dry] both branches build `length(_) == X`; could share one `putMetadata`. Not applied: it would rewrite the untouched initializer branch for no behaviour gain.
RefinementTypeChecker.java:256 - [name-meaningful] `l` for the dimension list is pre-existing; left alone to keep the diff small.
  • KISS / YAGNI: the fix removes a loop and a dead local (ep) instead of adding code; good.
  • Comments: the new comment says why only the first dimension counts (inner arrays are not modelled); no issue refs.
  • Tests: one line in each existing array-length fixture, both fail on main. Lean.

Full mvn test still passes after the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new T[a][b] gets length b instead of a (unsound)

1 participant